package com.senven.payorder.service.impl;

import com.senven.common.entity.*;
import com.senven.common.vo.CommonResult;
import com.senven.payorder.feign.AddressFeign;
import com.senven.payorder.feign.OrderFeign;
import com.senven.payorder.mapper.PayOrderMapper;
import com.senven.payorder.service.IPayOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsy
 * @since 2022-09-05
 */
@Service
public class PayOrderServiceImpl extends ServiceImpl<PayOrderMapper, PayOrder> implements IPayOrderService {

    @Autowired
    private PayOrderMapper payOrderMapper;

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private AddressFeign addressFeign;

    //添加到订单支付表
    @Override
    public CommonResult adds(List<Branch> list,String uid) {

        for (Branch b: list){
            List<ShoppingTrolley> goods = b.getGoods();
            for (ShoppingTrolley s: goods){

                int i1 = (int) (Math.random()*1000000000);
                Integer stId = s.getStId();
                Integer gid = s.getGid();
                Integer bid = s.getBid();
                Integer num = s.getStNum();
                Integer gprice = s.getGprice();
                Integer money = gprice*num;
                Boolean isDeleted=false;

                PayOrder payOrder = new PayOrder(i1,bid,gid,money,num,uid,isDeleted,stId);
                int i = payOrderMapper.insert(payOrder);
            }
        }

        return new CommonResult(2000,"添加成功",null);
    }

    @Override
    public Integer addApi(List<Branch> list,String uid) {
        Integer bid = null;
        Integer gid = null;
        Integer money = null;
        Integer num = null;
        Boolean isDeleted = null;
        Integer stId = null;

        for (Branch b: list){
            List<ShoppingTrolley> goods = b.getGoods();
            for (ShoppingTrolley s: goods){
                gid = s.getGid();
                bid = s.getBid();
                num = s.getStNum();
                Integer gprice = s.getGprice();
                stId = s.getStId();
                money = gprice*num;
                isDeleted = false;
            }
        }
        int i1 = (int) (Math.random()*1000000000);

        PayOrder payOrder = new PayOrder(i1,bid,gid,money,num,uid,isDeleted,stId);
        int i = payOrderMapper.insert(payOrder);
        return i;
    }

    //查询所有
    @Override
    public CommonResult selectAll(String uid) {

        //查询pay_order表中的所有数据
        List<PayOrder> list = payOrderMapper.selectAll(uid);
        //查询pay_order表中的餐厅信息
        List<Branch> blist = payOrderMapper.selectBranchName(uid);

        for (Branch b : blist) {
            List<PayOrder> payOrders = new ArrayList<>();
            for (PayOrder p : list) {
                if(b.getBranchid()==p.getBid()){
                    payOrders.add(p);
                    b.setPds(payOrders);
                }
            }

        }
        return new CommonResult(2000,"查询成功",blist);
    }

    //计算商品总和
    @Override
    public CommonResult selectSum(String uid) {
        Integer sum = payOrderMapper.selectSum(uid);
        return new CommonResult(2000,"查询成功",sum);
    }

    //获取当前时间
    @Override
    public CommonResult getData() {

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Date now = new Date();

        //预计送达时间
        long time = 30*60*1000;//30分钟
        Date afterDate = new Date(now .getTime() + time);//30分钟后的时间

        String timeArrival = sdf.format(afterDate);

        return new CommonResult(2000,"获取成功",timeArrival);
    }

    //添加到订单表
    @Override
    public CommonResult addOrder(List<Branch> list, Integer total, Integer payid,String address,String userId) {

        Integer branchid = null;

        List<PayOrder> goods = new ArrayList<>();
        for (Branch b: list){
            goods = b.getPds();
            for (PayOrder p: goods){
                branchid =  p.getBid();
            }
        }
        String paymodelid = String.valueOf(payid);
        String money = String.valueOf(total);
        Order order = new Order(null,null,paymodelid,null, LocalDateTime.now(),0,branchid,money,address,userId,goods);

        String orders = orderFeign.insertOrder(order);

        return new CommonResult(2000,"添加成功",orders);
    }

    //从菜品也添加到订单页面
    @Override
    public CommonResult addApi2(Integer branchid,Integer goodsid,Integer sum) {
        System.out.println("sum"+sum);
        if (sum==0){
            return new CommonResult(5001, "请先添加菜品", null);
        }else if(sum!=0){
            System.out.println("goodsid"+goodsid);
            if (goodsid==0){
                List<ShoppingTrolley> getCat = payOrderMapper.getCat(branchid, null);
                for (ShoppingTrolley s : getCat) {
                    int i1 = (int) (Math.random() * 1000000000);
                    Integer stId = s.getStId();
                    Integer gid = s.getGid();
                    Integer bid = s.getBid();
                    Integer num = s.getStNum();
                    Integer gprice = s.getGprice();
                    Integer money = gprice * num;
                    String uid = s.getUid();
                    Boolean isDeleted = false;
                    PayOrder payOrder = new PayOrder(i1,bid,gid,money,num,uid,isDeleted,stId);
                    int i = payOrderMapper.insert(payOrder);

                }
            }else if(goodsid!=0) {
                List<ShoppingTrolley> getCat = payOrderMapper.getCat(branchid, goodsid);
                for (ShoppingTrolley s : getCat) {
                    int i1 = (int) (Math.random() * 1000000000);
                    Integer stId = s.getStId();
                    Integer gid = s.getGid();
                    Integer bid = s.getBid();
                    Integer num = s.getStNum();
                    Integer gprice = s.getGprice();
                    Integer money = gprice * num;
                    String uid = s.getUid();
                    Boolean isDeleted = false;
                    PayOrder payOrder = new PayOrder(i1,bid,gid,money,num,uid,isDeleted,stId);
                    int i = payOrderMapper.insert(payOrder);

                }
            }
        }
        return null;
    }

    //逻辑删除
    @Override
    public CommonResult deleteIsDeleted(List<Integer> poIds) {

        Integer sum = 0;
        for (Integer id: poIds ) {
            Integer i = payOrderMapper.updatePoId(id);
            sum = i+sum;
        }
        if(sum>0){
            return new CommonResult(2000,"删除成功",sum);
        }
        return new CommonResult(5000,"逻辑删除失败",null);
    }

    //根据uid查询用户地址
    @Override
    public CommonResult selectUid(String uid) {
        List<Address> list = addressFeign.findAllAddressByUid(uid);
        return new CommonResult(2000,"查询成功",list);
    }
}





















